<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Promise extends AdminController{
    public $_validation = null;

    function __construct(){
        parent::__construct();
        $this->load->model('MPromise');
    }

    function index(){
        $this->load->library('FormFilter');
        $this->load->helper('formfilter');
        $this->formfilter->addFilter('nick_name', 'like');
        $this->formfilter->addFilter('status', 'where');

        if ($begintime = filterValue('user_auth_info_create_time_begin')) {
            $this->formfilter->addFilter('user_auth_info_create_time_begin', 'where', array('create_time >= ', strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = filterValue('user_auth_info_create_time_end')) {
            $this->formfilter->addFilter('user_auth_info_create_time_end', 'where', array('create_time <= ', strtotime($endtime . ' 23:59:59')));
        }
        $number_perpage = filterValue('user_auth_info_number_perpage');
        $total_number = $this->MPromise->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list  = $this->MPromise->getList($limit);
        $this->assign('list', $list);
        $this->layout();
    }

    public function return_confirm(){
        $data = $this->input->post();
        $item = $this->MPromise->getById($data['oid']);
        if ((int)$item->status !== 1){
            exit(json_encode([
                'status' => 0,
                'msg' => '当前用户未缴纳保证金',
                'data' => $item
            ]));
        }
        if ($this->db->update('user_promise',['status' => 3, 'return_time' => time()],['id' => $data['oid']]) && $this->db->update('user',['promise_status' => 0],['id' => $item->user_id])){
            exit(json_encode([
                'status' => 1,
                'msg' => '保证金退还成功'
            ]));
        }else{
            exit(json_encode([
                'status' => 0,
                'msg' => '保证金退还失败'
            ]));
        }
    }
}
